﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Web.Common
{
    public class Common
    {
        public static  Models.Common.InitInfo GetInitInfoModel()
        {
            Models.Common.InitInfo info = new Models.Common.InitInfo();
            info.homeInfo = new Models.Common.HomeInfoModel();
            info.homeInfo.href = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='HomeHref'").ToString();
            info.homeInfo.title = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='HomeTitle'").ToString();
            info.logoInfo = new Models.Common.LogoInfoModel();
            info.logoInfo.href = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoHref'").ToString();
            info.logoInfo.image = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoIcon'").ToString();
            info.logoInfo.title = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoTitle'").ToString();
            info.menuInfo = new List<Models.Common.ChildModel>();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Menus where ParentID=0").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                Models.Common.ChildModel m = new Models.Common.ChildModel();
                m.title = item["Title"].ToString();
                m.target = "_self";
                m.icon = item["Icon"].ToString();
                m.href = item["Href"].ToString();
                m.child = new List<Models.Common.ChildModel>();
                Web.Common.Common.AddMenuChild(m, Convert.ToInt32(item["ID"]));
                info.menuInfo.Add(m);
            }
            return info;
        }
        public static void AddMenuChild(Models.Common.ChildModel m,int ID)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Menus where ParentID="+ID.ToString()).Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                Models.Common.ChildModel mm = new Models.Common.ChildModel();
                mm.title = item["Title"].ToString();
                mm.target = "_self";
                mm.icon = item["Icon"].ToString();
                mm.href = item["Href"].ToString();
                mm.child = new List<Models.Common.ChildModel>();
                AddMenuChild(mm, Convert.ToInt32(item["ID"]));
                m.child.Add(mm);
            }
        }

        public static string GetHerdeerConnString(System.Net.Http.Headers.HttpRequestHeaders headers,out string connectionString)
        {
            string strSerrver = string.Empty;
            string strDatabase = string.Empty;
            string strUid = string.Empty;
            string strPwd = string.Empty;

            try
            {
                connectionString = "";

                if (headers.Contains("Server"))
                {
                    strSerrver = headers.GetValues("Server").First();
                    if (strSerrver.Length <= 0)
                        return "Headers参数Server不可为空";
                }
                else
                {
                    return "缺少Headers参数Server";
                }

                if (headers.Contains("Database"))
                {
                    strDatabase = headers.GetValues("Database").First();
                    if (strDatabase.Length <= 0)
                        return "Headers参数Database不可为空";
                }
                else
                {
                    return "缺少Headers参数Database";
                }

                if (headers.Contains("Uid"))
                {
                    strUid = headers.GetValues("Uid").First();
                    if (strUid.Length <= 0)
                        return "Headers参数Uid不可为空";
                }
                else
                {
                    return "缺少Headers参数Uid";
                }

                if (headers.Contains("Pwd"))
                {
                    strPwd = headers.GetValues("Pwd").First();
                    if (strPwd.Length <= 0)
                        return "Headers参数Uid不可为空";
                }
                else
                {
                    connectionString = "";
                    return "缺少Headers参数Pwd";
                }

                connectionString = "Server=" + strSerrver + ";Database=" + strDatabase + ";Uid=" + strUid + ";Pwd=" + strPwd;

            }
            catch (Exception)
            {
                throw;
            }
            return "OK";
        }

    }
}